package com.angel.utils;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/***
 * 发送短信验证码的接口工具
 */
public class MessageHttpUtils {
    /***
     * 发送短信验证码工具
     * @param phone 电话号码
     * @param code 验证码
     * @param ptl_id 验证码模板形式,myconfig类提供
     * @param appcode myconfig类提供
     * @return 返回jsion字符串：发送成功{"return_code": "00000","order_id": "ALY15........825"}
     *          发送失败{" return_code": "10000"}
     */
    public static String sendMessage(String phone, String code, String ptl_id,String appcode) {
        String url = "http://dingxin.market.alicloudapi.com/dx/sendSms";
//      String appcode = "5746a7238d014057998b2e84ed373342";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "APPCODE " + appcode);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
        params.add("mobile", phone);
        params.add("param", "code:" + code);
        params.add("tpl_id", ptl_id);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        return response.getBody();
    }

//    public static void main(String[] args) {
//        String msg =MessageHttpUtils.sendMessage("12345678900","45678","TP1711063","5746a7238d014057998b2e84ed373342");
//        System.out.println(msg);
//    }

}
